//
// Created by Thinkpad on 2025/10/29.
//

#include <cstring>
#include <iostream>
using namespace std;

int main() {
    // Find the length of a string
    const char message1 [] {"The sky is blue."};

    const char* message2 {"The sky is blue."};
    cout << message1 << endl;

    // strlen ignores null character
    cout << "strlen(message1): " << strlen(message1) << endl;
    // Includes the null character
    cout << "sizeof(message1): " << sizeof(message1) << endl;

    cout << "strlen(message2): " << strlen(message2) << endl;
    cout << "sizeof(message2): " << sizeof(message2) << endl;

    // strcmp:comparing strings
    cout << endl;
    cout << "strcmp: " << endl;
    const char * string_date1 {"Alabama"};
    const char * string_date2 {"Blabama"};

    // Print out the comparison
    cout << "strcmp (" << string_date1 << "," <<  string_date2 << ") "
    << strcmp(string_date1, string_date2) << endl;

    string_date1 = "Alabama";
    string_date2 = "Alabama";

    // Print out the comparison
    cout << "strcmp (" << string_date1 << "," <<  string_date2 << ") "
    << strcmp(string_date1, string_date2) << endl;

    // Find first occurrence
    const char *str {"Try not.Do,or do not.There is no try."};
    char target = 'T';
    const char *result = str;
    int iterations{};

    while ((result = strchr(result,target)) != nullptr){
        cout << "Found : " << target << "'starting at '" << result << "'" << endl;

        ++result;
        ++iterations;
    }

    cout << "iterations: " << iterations << endl;

    // Find last occurrence
    cout << endl;
    cout << "strrchr : " << endl;
    char input[] ="/home/user/hello.cpp";
    char* output = strrchr(input,'/');
    if (output) {
        cout << output + 1 << endl;
    }

    // Joining strings

    cout << endl;
    cout << "strcat : " << endl;
    char dest[50] ="hello";
    char src[50] ="world";
    strcat(dest,src);
    strcat(dest," goodbye world");

    cout << "dest : " << dest << endl;

    // Copying strings
    cout << endl;
    const char* source3 ="C++ is a multipurpose programming language";
    char *dest3 = new char[strlen(source3) + 1];

    strcpy(dest3,source3);
    cout << "sizeof(dest3): " << sizeof(dest3) << endl;
    cout << "strlen(dest3): " << strlen(source3) << endl;
    cout << "dest3: " << dest3 << endl;

    cout << endl;


    return 0;
}